<template>
  <div class="container">
    <div class="title">
      <span v-if="!contractParams.cid">合同信息</span>
      <span v-else>{{formData.contract_name}}——{{formData.contract_code}}</span>
    </div>
    <el-form :model="formData" ref="form" :rules="rules" :inline="false" size="normal" label-position="top">
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item label="合同名称" prop="contract_name">
            <el-input v-model="formData.contract_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="合同金额" prop="contract_amount">
            <el-input type="number" v-model="formData.contract_amount">
              <span slot="append">万元</span>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item label="竣工及验收日期" prop="check_time">
            <el-date-picker v-model="formData.check_time" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
              :clearable="false" class="wid">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="免费运维截止日期" prop="nopay_limit">
            <el-date-picker v-model="formData.nopay_limit" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
              :clearable="false" class="wid">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item prop="project_contract_info">
            <up-load titleName="验收文件" :size="50" :require="true" v-model="formData.project_contract_info"></up-load>
          </el-form-item>
          <el-button type="primary" size="medium" @click="ysShow = true" v-if="this.switch">查看示例</el-button>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item prop="project_contract">
            <up-load titleName="采购合同" :size="50" :require="true" v-model="formData.project_contract"></up-load>
          </el-form-item>
          <el-button type="primary" size="medium" @click="cgShow = true" v-if="this.switch">查看示例</el-button>
        </el-col>
      </el-row>
      <br>
      <div class="title">最近一次合同运维信息</div>
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item label="是否第一次申请项目运维" prop="first_apply">
            <el-radio-group v-model="formData.first_apply">
              <el-radio-button label="1" border>是</el-radio-button>
              <el-radio-button label="0" border>否</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <template v-if="formData.first_apply ==='0'">
          <el-col :span="8" :offset="0">
            <el-form-item label="运维合同名称" prop="name">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="0">
            <el-form-item label="运维合同金额" prop="amount">
              <el-input v-model="formData.amount">
                <span slot="append">万元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
      <template v-if="formData.first_apply ==='0'">
        <el-row :gutter="20">
          <el-col :span="8" :offset="0">
            <el-form-item label="运维服务提供商" prop="maintain_service_apply">
              <el-input v-model="formData.maintain_service_apply"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="0">
            <el-form-item label="运维合同开始时间" prop="start_time">
              <el-date-picker v-model="formData.start_time" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                :clearable="false" class="wid">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="0">
            <el-form-item label="运维合同结束时间" prop="end_time">
              <el-date-picker v-model="formData.end_time" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                :clearable="false" class="wid">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item prop="maintenance_contract">
              <up-load titleName="运维合同" :size="50" :require="true" v-model="formData.maintenance_contract"></up-load>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </el-form>
    <div class="center">
      <el-button type="danger" @click="contractCancel" :loading="saveLoading">取消</el-button>
      <template v-if="this.switch && this.copyStatus!='0'">
        <el-button type="primary" @click="contractEdit" :loading="saveLoading" v-if="contractParams.cid">修改
        </el-button>
        <el-button type="primary" @click="contractAdd" :loading="saveLoading" v-if="contractParams.pid">添加
        </el-button>
      </template>
    </div>
    <!-- 验收文件 -->
    <el-dialog title="验收文件" :visible.sync="ysShow" width="70%" @close="ysShow = false" append-to-body top="0">
      <img src="@/assets/images/example/caigouhetong.png" class="wid">
      <span slot="footer">
        <el-button type="danger" @click="ysShow = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 采购合同 -->
    <el-dialog title="采购合同" :visible.sync="cgShow" width="70%" @close="cgShow = false" append-to-body top="0">
      <img src="@/assets/images/example/yanshoubaogao.png" class="wid">
      <span slot="footer">
        <el-button type="danger" @click="cgShow = false">关闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import UpLoad from '../Upload'
export default {
  name: 'Contract',
  components: {
    UpLoad
  },
  props: {
    contractParams: {
      type: Object,
      default: () => {
        return {}
      }
    },
    switch: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    copyStatus: {
      type: String,
      default: () => {
        return '3'
      }
    },
  },
  data () {
    return {
      saveLoading: false,
      formData: {
        first_apply: '0'
      },
      rules: {
        contract_name: [
          { required: true, message: '请填写合同名称', trigger: 'blur' }
        ],
        contract_amount: [
          { required: true, validator: this.$util.isFloat, trigger: 'blur' }
        ],
        check_time: [
          { required: true, message: '请选择竣工及验收日期', trigger: 'change' }
        ],
        nopay_limit: [
          { required: true, message: '请选择免费运维截止日期', trigger: 'change' }
        ],
        project_contract_info: [
          { required: true, message: '请上传验收文件', trigger: 'blur' }
        ],
        project_contract: [
          { required: true, message: '请上传采购合同', trigger: 'blur' }
        ],
        first_apply: [
          { required: true, message: '请选择是否第一次申请项目运维', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请填写运维合同名称', trigger: 'blur' }
        ],
        amount: [
          { required: true, validator: this.$util.isFloat, trigger: 'blur' }
        ],
        maintain_service_apply: [
          { required: true, message: '请填写运维服务提供商', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '请选择运维合同开始时间', trigger: 'change' }
        ],
        end_time: [
          { required: true, message: '请选择运维合同结束时间', trigger: 'change' }
        ],
        maintenance_contract: [
          { required: true, message: '请录入运维合同', trigger: 'blur' }
        ],
      },
      ysShow: false,
      cgShow: false
    }
  },
  created () {

  },
  mounted () {
    if (this.contractParams.cid) {
      this.init()
    }
  },
  methods: {
    async init () {
      const res = await this.$http('inputContractObj', {
        id: this.contractParams.cid
      })
      this.formData = res
    },
    contractCancel () {
      this.$emit('cancel')
    },
    async contractEdit () {
      if (!this.check()) {
        return
      }
      this.saveLoading = true
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const res = await this.$http('inputContractUp', {
            project_id: this.contractParams.pid,
            del_flag: '0',
            level: '0',
            ...this.formData
          })
          this.$tips.success(res.errmsg)
          this.saveLoading = false
          this.$emit('cancel')
          this.$emit('init')
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      });
    },
    async contractAdd () {
      if (!this.check()) {
        return
      } this.saveLoading = true
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const res = await this.$http('inputContractAdd', {
            project_id: this.contractParams.pid,
            del_flag: '0',
            ...this.formData
          })
          this.$tips.success(res.errmsg)
          this.saveLoading = false
          this.$emit('cancel')
          this.$emit('init')
        } else {
          this.$tips.error('请检查必填项')
          this.saveLoading = false
          return false;
        }
      });
    },
    check () {
      if (this.formData.nopay_limit < this.formData.check_time) {
        this.$tips.error('竣工及验收日期应大于免费运维截止日期')
        return false
      }
      if (this.formData.first_apply === '0' && (this.formData.end_time < this.formData.start_time)) {
        this.$tips.error('运维合同开始时间应大于运维合同结束时间')
        return false
      }
      return true
    }
  },
  computed: {

  },
}
</script>
<style lang='scss' scoped>
</style>
